//
//  ServerInterface.m
//  Contacts
//
//  Created by 李 雷川 on 14-7-19.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "ServerInterface.h"
#import "Contact.h"
#import "ContactDao.h"
#import "DepartmentDao.h"
static NSString * const AFAppDotNetAPIBaseURLString = @"http://api.app.jizhehome.com/mobile/";
//static NSString * const AFAppDotNetAPIBaseURLString = @"http://192.168.0.99:88/mobile/";
@implementation ServerInterface

+(NSString *) URLEncodeString:(NSString *) str
{
    
    NSMutableString *tempStr = [NSMutableString stringWithString:str];
    [tempStr replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])];
    
    
    return [[NSString stringWithFormat:@"%@",tempStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

+(ServerInterface *) sharedServerInterface{
    static ServerInterface *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[ServerInterface alloc] initWithBaseURL:[NSURL URLWithString:AFAppDotNetAPIBaseURLString]];
        _sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    });

    return _sharedClient;
}

//检查客户端更新状态
+(NSURLSessionDataTask *)checkNewClient:(NSDictionary *)paramDic   WithBlock:(void (^)(NSDictionary *posts, NSError *error))block{
    return [[ServerInterface sharedServerInterface] GET:@"version/update.json" parameters:paramDic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSLog(@"Json is:%@",JSON);
        if (block) {
            block(JSON, nil);
        }
    }failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSDictionary dictionary], error);
        }
    }];
}


//获取所有联系人
+(NSURLSessionDataTask *)getContactsWithContactDis:(NSDictionary *)paramDic   WithBlock:(void (^)(NSDictionary *posts, NSError *error))block{

    return [[ServerInterface sharedServerInterface] GET:@"company_member.c?" parameters:paramDic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if (block) {
            block(JSON, nil);
        }
     }failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSDictionary dictionary], error);
        }
     }];
}

//登录
+(NSURLSessionDataTask *)loginWithInfoDic:(NSDictionary*)paramDic  withBlock:(void (^)(NSDictionary *userInfoDic, NSError *error))block{
    
    return [[ServerInterface sharedServerInterface] GET:@"login.c?" parameters:paramDic  success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if (block) {
            block([NSDictionary dictionaryWithDictionary:JSON], nil);
        }
    }failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSDictionary dictionary], error);
        }
    }];

}

//上传头像
+(NSURLSessionDataTask *)uploadHeadWithDic:(NSDictionary*)paramDic withFileName:(NSString *)fileName withBlock:(void (^)(NSDictionary *userInfoDic, NSError *error))block{
    NSURL *filePath = [NSURL fileURLWithPath:[IMAGE_FORDER stringByAppendingPathComponent:fileName]];
    return [[ServerInterface sharedServerInterface]POST:@"upload/head" parameters:paramDic
        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        BOOL success =[formData appendPartWithFileURL:filePath name:@"img_file" fileName:fileName mimeType:@"image/jpeg" error:nil];
            if (success) {
                NSLog(@"success");
            }
    }success:^(NSURLSessionDataTask *task, id responseObject) {
        if (block) {
            block([NSDictionary dictionaryWithDictionary:responseObject], nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (block) {
            block([NSDictionary dictionary], error);
        }
    }];
    
}

//下载图片
+(NSURL *)downloadImageWithUrlPath:(NSString*)urlPath withBlock:(void (^)(UIImage *image, NSError *error))block{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:urlPath];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *imageURL = [NSURL fileURLWithPath:IMAGE_FORDER];
        return [imageURL URLByAppendingPathComponent:[response suggestedFilename]];     
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        NSLog(@"File downloaded to: %@", filePath);
    }];
    [downloadTask resume];
    return nil;
}

//更新用户信息
+(NSURLSessionDataTask *)updateUserInfoWithInfoDic:(NSDictionary*)paramDic  withBlock:(void (^)(NSDictionary *userInfoDic, NSError *error))block{
    return [[ServerInterface sharedServerInterface] GET:@"update_user_info.c?" parameters:paramDic  success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if (block) {
            block([NSDictionary dictionaryWithDictionary:JSON], nil);
        }
    }failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSDictionary dictionary], error);
        }
    }];
}

//重置密码
+(NSURLSessionDataTask *)resetPasswordWithInfoDic:(NSDictionary*)paramDic  withBlock:(void (^)(NSDictionary *userInfoDic, NSError *error))block{
    return [[ServerInterface sharedServerInterface] GET:@"update_password.c?" parameters:paramDic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if (block) {
            block([NSDictionary dictionaryWithDictionary:JSON], nil);
        }
    }failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSDictionary dictionary], error);
        }
    }];
}

//新闻列表
+(NSURLSessionDataTask *)getFirstIndexNewsWithDic:(NSDictionary*)paramDic  withBlock:(void (^)(NSDictionary *newDic, NSError *error))block{
    return [[ServerInterface sharedServerInterface] GET:@"index_first_new.c?" parameters:paramDic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if (block) {
            block([NSDictionary dictionaryWithDictionary:JSON], nil);
        }
    }failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSDictionary dictionary], error);
        }
    }];
}

//类别
+(NSURLSessionDataTask *)getCategoryWithDic:(NSDictionary*)paramDic  withBlock:(void (^)(NSDictionary *categoryDic, NSError *error))block{
    return [[ServerInterface sharedServerInterface] GET:@"get_message_category.c?" parameters:paramDic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if (block) {
            block([NSDictionary dictionaryWithDictionary:JSON], nil);
        }
    }failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSDictionary dictionary], error);
        }
    }];
}

//类别下对应的所有消息
+(NSURLSessionDataTask *)getCategoryNoticesWithDic:(NSDictionary*)paramDic  withBlock:(void (^)(NSDictionary *categoryDic, NSError *error))block{
    return [[ServerInterface sharedServerInterface] GET:@"all_new_message.c?" parameters:paramDic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if (block) {
            block([NSDictionary dictionaryWithDictionary:JSON], nil);
        }
    }failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSDictionary dictionary], error);
        }
    }];
}

//获取消息或者活动详情
+(NSURLSessionDataTask *)getNoticeDetailWithDic:(NSDictionary*)paramDic  withBlock:(void (^)(NSDictionary *noticeDetailDic, NSError *error))block{
    return [[ServerInterface sharedServerInterface] GET:@"get_one_message.c?" parameters:paramDic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if (block) {
            block([NSDictionary dictionaryWithDictionary:JSON], nil);
        }
    }failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSDictionary dictionary], error);
        }
    }];
}

//反馈
+(NSURLSessionDataTask *)feedbackWithDic:(NSDictionary*)paramDic  withBlock:(void (^)(NSDictionary *noticeDetailDic, NSError *error))block{
    return [[ServerInterface sharedServerInterface] GET:@"save_feed_back.c?" parameters:paramDic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        if (block) {
            block([NSDictionary dictionaryWithDictionary:JSON], nil);
        }
    }failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSDictionary dictionary], error);
        }
    }];
}
@end
